package com.ityuhao.tech.concurrency;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Slf4j
@Component
public class ConsumeDemo implements InitializingBean {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void afterPropertiesSet() {
        Thread t = new Thread(() -> {
            while (true) {
                try {
                    String value = redisTemplate.opsForList().leftPop("channel01");
                    if (StringUtils.isEmpty(value)){
                        // redis list 无数据 等待500ms
                        Thread.sleep(1000);
                        log.info("channel01 value is empty");
                    }else {
                        log.info("consume value:{}",value);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }
}
